package admin.forms;

import java.net.URL;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import entities.AutoSite;

public class AutoSiteForm {
	public static TreeMap checkValid(HttpServletRequest request, AutoSite autoSite) {
		TreeMap form = new TreeMap();
		TreeMap checkValid = new TreeMap();
		
		String url = (String) request.getParameter("url");
		String getPage = (String) request.getParameter("getPage");
		String hotNews = (String) request.getParameter("hotNews");
		String regLink = (String) request.getParameter("regLink");
		String scheduleSite = (String) request.getParameter("scheduleSite");
		String category = (String) request.getParameter("category");
		String scheduleSitePage = (String) request.getParameter("scheduleSitePage");
		String sitePage = (String) request.getParameter("sitePage");
		String currentPage = (String) request.getParameter("currentPage");
		String totalPage = (String) request.getParameter("totalPage");
		String status = (String) request.getParameter("status");
		
		if (url == null || url.equals("")) {
			checkValid.put("url", "Site is not empty!");
		} else {
			try {
				URL surl = new URL(url);
			} catch (Exception e) {
				checkValid.put("url", "Site is not url valid!");
			}
			autoSite.setUrl(url);
		}
		
		if (getPage != null)
			autoSite.setGetPage(new Boolean(getPage));
		
		if (regLink == null || regLink.equals("")) {
			checkValid.put("regLink", "Title link is not empty!");
			autoSite.setRegLink("");
		} else {
			autoSite.setRegLink(regLink);
		}
			
		if (category != null && !category.equals(""))
			autoSite.setCategory(category);
		
		if (scheduleSite == null || scheduleSite.isEmpty()) {
			checkValid.put("scheduleSite", "Time schedule site is not emtpy");
		} else {
			try {
				autoSite.setScheduleSite(Integer.parseInt(scheduleSite));
			} catch (Exception e) {
				checkValid.put("scheduleSite", "Time schedule site must be a number!");
			}
		}
		
		if (autoSite.isGetPage() && (scheduleSitePage == null || scheduleSitePage.equals(""))) {
			checkValid.put("scheduleSitePage", "Time schedule page is not empty!");
		} else if (autoSite.isGetPage()) {
			try {
				autoSite.setScheduleSitePage(Integer.parseInt(scheduleSitePage));
			} catch (Exception e) {
				checkValid.put("scheduleSitePage", "Time schedule page must be a number!");
			}
		}
		
		if (autoSite.isGetPage() && (sitePage == null || sitePage.equals(""))) {
			checkValid.put("sitePage", "Site pages is not empty!");
		} else {
			autoSite.setSitePage(sitePage);
		}
		
		if (autoSite.isGetPage() && (currentPage == null || currentPage.equals(""))) {
			checkValid.put("currentPage", "Current page is not empty!");
		} else if (autoSite.isGetPage()) {
			try {
				autoSite.setCurrentPage(Integer.parseInt(currentPage));
			} catch (Exception e) {
				checkValid.put("currentPage", "Current page must be a number!");
			}
		}
		
		if (autoSite.isGetPage() && (totalPage == null || totalPage.equals(""))) {
			checkValid.put("totalPage", "Total page is not empty!");
		} else if (autoSite.isGetPage()) {
			try {
				autoSite.setTotalPage(Integer.parseInt(totalPage));
			} catch (Exception e) {
				checkValid.put("totalPage", "Total page must be a number!");
			}
		}
		
		form.put("checkValid", checkValid);
		form.put("autoSite", autoSite);
		return form;
	}
	
}
